<template>
	<view class="index">
		<u-search @custom="tosearch()" @search="tosearch" v-model="value"></u-search>
		<view class="lunbo">
			<swiper indicator-dots :circular="true" interval="3000" :autoplay="true">
				<swiper-item v-for="item in lun" :key="item.id" @click="tonewsdetail(item)">
					<image :src="'http://124.93.196.45:10001'+item.advImg"></image>
				</swiper-item>
			</swiper>
		</view>
		<text style="font-size: 25px;">热门主题</text>
		<view class="hottopic">
			<u-grid col="5">
				<u-grid-item v-for="(item,index) in serve" :key="item.id" v-if="index<9" @click="toservedetail">
					<image :src="'http://124.93.196.45:10001'+item.imgUrl" style="width: 50px; height: 50px;"></image>
					<text style="font-size: 10px;">{{item.serviceName}}</text>
				</u-grid-item>
				<u-grid-item @click="Toserve">
					<image src='../../static/DM_20231009141943_001.png' style="width: 50px; height: 50px;"></image>
					<text style="font-size: 12px;">全部服务</text>
				</u-grid-item>
			</u-grid>
		</view>
		<text style="font-size: 25px;">热点新闻</text>
		<view class="hot">
			<view class="news" v-for="item in hotnews" :key="item.id" @click="tonewsdetail(item)">
				<image :src="'http://124.93.196.45:10001'+item.cover"></image>
				<view class="newsboot">
					<rich-text :nodes="getreach(item.title||'')"></rich-text>
				</view>
			</view>
		</view>
		<text style="font-size: 25px;">新闻专栏</text>
		<u-tabs :list="type" @click="gettypenews"></u-tabs>
		<view class="allnews">
			<view class="n1" v-for="item in news" :key="item.id" @click="tonewsdetail(item)">
				<image :src="'http://124.93.196.45:10001'+item.cover"></image>
				<view class="n2">
					<text>{{item.title}}</text>
				</view>
				<view class="n3">
					<text>{{item.content}}</text>
				</view>
				<view class="n4">
					<text>评论：{{item.commentNum}}</text>
					<text>发布时间：{{item.publishDate}}</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				lun:[],
				serve:[],
				hotnews:[],
				type:[],
				news:[],
				value:''
			}
		},
		onLoad() {
			this.getlunbo()
			this.getserve()
			this.gethotnews()
			this.gettype()
			this.getallnews()
		},
		methods: {
			getlunbo(){
				this.api.get('/prod-api/api/rotation/list').then(res=>{
					this.lun=res.data.rows
				})
			},
			getserve(){
				this.api.get('/prod-api/api/service/list').then(res=>{
					this.serve=res.data.rows
				})
			},
			gethotnews(){
				this.api.get('/prod-api/press/press/list?hot=Y').then(res=>{
					this.hotnews=res.data.rows
				})
			},
			getreach(val){
				return "<div style='overflow: hidden; text-overflow: ellipsis; white-space: nowrap;'>"+val+"</div>"
			},
			gettype(){
				this.api.get('/prod-api/press/category/list').then(res=>{
					this.type=res.data.data
				})
			},
			getallnews(){
				this.api.get('/prod-api/press/press/list').then(res=>{
					this.news=res.data.rows
				})
			},
			gettypenews(item){
				console.log(item)
				this.api.get('/prod-api/press/press/list?type='+item.id).then(res=>{
					this.news=res.data.rows
				})
			},
			Toserve(){
				uni.switchTab({
					url:'/pages/serve/serve'
				})
			},
			tosearch(){
				if(this.value===''){
					uni.navigateTo({
						url:'/pages/index/search/search'
					})
				}else{
					uni.navigateTo({
						url:'/pages/index/haveserch/haveserch'
					})
					this.value=''
				}
			},
			tonewsdetail(item){
				uni.navigateTo({
					url:'/pages/newsdetail/newsdetail?id='+item.id
				})
			},
			toservedetail(index){
				console.log(index)
				switch(index){
					case 1:
						uni.navigateTo({
							url:'/pages/serve/jiaoyu/jiaoyu'
						})
						break
					case 2:
						uni.navigateTo({
							url:'/pages/serve/zhengfu/zhengfu'
						})
						break
					case 3:
						uni.navigateTo({
							url:'/pages/serve/libary/libary'
						})
						break
					case 4:
						uni.navigateTo({
							url:'/pages/serve/laji/laji'
						})
						break
					case 5:
						uni.navigateTo({
							url:'/pages/serve/volunteer/volunteer'
						})
						break
					case 6:
						uni.navigateTo({
							url:'/pages/serve/stop/stop'
						})
						break
					case 7:
						uni.navigateTo({
							url:'/pages/serve/subway/subway'
						})
						break;
					case 8:
						uni.navigateTo({
							url:'/pages/serve/bus/bus'
						})
						break
					case 9:
						uni.navigateTo({
							url:'/pages/serve/hospital/hospital'
						})
						break
				}
			}
		}
	}
</script>

<style lang="scss">
	.lunbo{
		image{
			width: 100%;
			height: 100%;
		}
	}
	.hottopic{
		margin-top: 10px;
		line-height: 40px;
	}
	.hot{
		display: flex;
		justify-content: space-between;
		margin-top: 10px;
		margin-bottom: 10px;
		view{
			width: 45%;
		}
		.news{
			image{
				width:100%;
				height: 175px;
				border-radius: 15px;
			}
		}
		.newsboot{
			width: 175px;
		}
	}
	.allnews{
		.n1{
			line-height: 30px;
			margin-top: 20px;
			image{
				width: 100%;
				height: 200px;
			}
		}
		.n2,.n3{
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}
		.n4{
			margin-top: 10px;
			display: flex;
			justify-content: space-between;
		}
	}
</style>
